var $;
layui.config({
    base: "js/"
}).use(['form', 'layer', 'jquery'], function () {
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage;
    $ = layui.jquery;

    $(".pic").click(function () {

        var index = layui.layer.open({
            title: "选择图片",
            type: 2,
            area: ['600px', '360px'],
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            content: "http://localhost/ci/admin/menu/select",
            success: function (layero, index) {
                layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                    tips: 3
                });
            }
        })

    })

    var addMenu = [];
    form.on("submit(addMenu)", function (data) {
        var obj = document.all.authority;
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked == true) {
                $('.menu_id').val(parseInt(obj[i].id.substring(10)));
            }
        }
        //console.log($('.menu_id').val());

        //是否添加过信息
        //addMenu = '{"role_name":"'+ $('.roleName').val() + '","remark":"'+ $('.remark').val() +'","create_time":"'+ formatTime(new Date()) +'"}';
        addMenu = '{"parent_id":"' + $('.menu_id').val() + '","name":"' + $('.name').val() + '","url":"' + $('.url').val() + '","perms":"' + $('.perms').val() + '","type":"' + $('.type').val() + '","icon":"' + $('.icon').html() + '"}';

        $.ajax({
            url: "http://localhost/ci/admin/menu/imp_add",
            type: "post",
            data: {'data': addMenu, 'token': localStorage.token},
            dataType: "json",
            success: function (data) {
                console.log(data);
                //top.layer.close(index);
                top.layer.msg("角色添加成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            },
            error: function (data) {
                console.log(data);
                //top.layer.close(index);
                top.layer.msg("角色添加失败！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }
        })
        return false;
    })

})

